package aiku.numericalcontroloa.Service.CompanyCulture.Impl;

import aiku.numericalcontroloa.Base.Utils.HttpStatus;
import aiku.numericalcontroloa.Base.Utils.Result;
import aiku.numericalcontroloa.Mapper.CompanyCulture.LearnRecordMapper;
import aiku.numericalcontroloa.Mapper.CompanyCulture.MaterialMapper;
import aiku.numericalcontroloa.Model.Dto.CompanyCulture.LearnRecordDto;
import aiku.numericalcontroloa.Model.Entity.LearnRecordGroup;
import aiku.numericalcontroloa.Model.Entity.CompanyCulture.LearnRecord;
import aiku.numericalcontroloa.Model.Entity.CompanyCulture.Material;
import aiku.numericalcontroloa.Model.Vo.CompanyCulture.LearnRecordVo;
import aiku.numericalcontroloa.Service.CompanyCulture.LearnRecordService;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author 黄广庭
 * @Date 2024/1/25 11:10
 */
@Service
public class LearnRecordServiceImpl implements LearnRecordService {
    private final LearnRecordMapper learnRecordMapper;
    private final MaterialMapper materialMapper;

    public LearnRecordServiceImpl(LearnRecordMapper learnRecordMapper, MaterialMapper materialMapper) {
        this.learnRecordMapper = learnRecordMapper;
        this.materialMapper = materialMapper;
    }

    @Override
    public Result getLearnRecordList(LearnRecordDto learnRecordDto) {
        if (learnRecordDto == null || (learnRecordDto.getCurrentPage() == null
                && learnRecordDto.getPageSize() == null))
            return new Result(HttpStatus.BAD_REQUEST, "缺少参数");
        learnRecordDto.setOffset(
                (learnRecordDto.getCurrentPage() * learnRecordDto.getPageSize()) - learnRecordDto.getPageSize());
        Map<String, Object> map = new HashMap<>();
        Integer total = learnRecordMapper.countLearnRecordList(learnRecordDto);
        map.put("total", total);
        List<LearnRecordDto> learnRecordDtoList = learnRecordMapper.fuzzySelectPage(learnRecordDto);
        List<LearnRecordVo> learnRecordVoList = new ArrayList<>();
        if (learnRecordDtoList.size() > 0) {
            learnRecordDtoList.forEach(item -> {
                LearnRecordVo learnRecordVo = new LearnRecordVo();
                BeanUtils.copyProperties(item, learnRecordVo);
                // 处理时间
                if (item.getCompleteTime() != null) {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    learnRecordVo.setCompleteTime(sdf.format(item.getCompleteTime()));
                }
                learnRecordVoList.add(learnRecordVo);
            });
        }
        map.put("learnRecordVoList", learnRecordVoList);
        return Result.success("查询成功", map);
    }

    @Override
    public Result getLearnRecordGropList(LearnRecordDto learnRecordDto) {
        if (learnRecordDto == null)
            return new Result(HttpStatus.BAD_REQUEST, "缺少参数");
        List<LearnRecordGroup> list = learnRecordMapper.countUserGroupList(learnRecordDto.getMaterialId());
        Map<String, Object> map = new HashMap<>();
        map.put("learnRecordVoList", list);
        return Result.success("查询成功", map);
    }

    @Override
    public Result getLearnRecord(LearnRecordDto learnRecordDto) {
        if (learnRecordDto == null) {
            return new Result(HttpStatus.BAD_REQUEST, "缺少参数");
        }
        if (learnRecordDto.getMaterialId() == null && learnRecordDto.getMaterialId().isEmpty()) {
            return new Result(HttpStatus.BAD_REQUEST, "缺少参数");
        }
        Material material = materialMapper.selectById(learnRecordDto.getMaterialId());
        if (material == null) {
            return new Result(HttpStatus.BAD_REQUEST, "该资料不存在");
        }
        // 根据资料id查自己的学习记录
        LambdaQueryWrapper<LearnRecord> learnRecordLQW = new LambdaQueryWrapper<>();
        learnRecordLQW.eq(LearnRecord::getMaterialId, material.getId())
                .eq(LearnRecord::getUserId, StpUtil.getLoginIdAsString());
        LearnRecord learnRecord = learnRecordMapper.selectOne(learnRecordLQW);
        if (learnRecord == null) {
            return new Result(HttpStatus.BAD_REQUEST, "该学习记录不存在");
        }
        return Result.success("查询成功", learnRecord);
    }

    @Override
    public Result changeStatus(LearnRecordDto learnRecordDto) {
        if (learnRecordDto == null) {
            return new Result(HttpStatus.BAD_REQUEST, "缺少参数");
        }
        LearnRecord learnRecord = learnRecordMapper.selectById(learnRecordDto.getId());
        if (learnRecord == null) {
            return new Result(HttpStatus.BAD_REQUEST, "该学习记录不存在");
        }
        // 修改学习记录已完成
        learnRecord.setStatus(1);
        learnRecord.setCompleteTime(new Date());
        learnRecordMapper.updateById(learnRecord);
        return Result.success("修改成功");
    }
}
